#include "numberitem.h"
#include "toolkit.h"
#include "resources.h"
#include <QPainter>

NumberItem::NumberItem():
    m_numarr(0),
    m_bRun(true),
    m_bFadeIn(true)
{
    m_numImage = ToolKit::m_rc->m_NumImageArr[static_cast<int>(ToolKit::m_curlevel / 5)];
    m_timer.setInterval(100);
    connect(&m_timer,SIGNAL(timeout()),this,SLOT(ShowNum()));
}

void NumberItem::ShowNum()
{
    if(m_bRun)
    {
        if(m_bFadeIn)
        {
            setOpacity(opacity() + 0.1);
            if(opacity() >= 1.0)
            {
                m_bFadeIn = false;
            }
        }
        else
        {
            setOpacity(opacity() - 0.1);
            if(opacity() <= 0.0)
            {
                m_bFadeIn = true;
                m_timer.stop();
                setVisible(false);
            }
        }
    }
}

void NumberItem::Start()
{
    m_bFadeIn = true;
    m_bRun = true;
    setOpacity(0.0);
    setVisible(true);
    m_timer.start();
}

void NumberItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if(m_numarr[0] == 1)
    {
        painter->drawPixmap(QRectF(0.0,0.0,9.0,12.0),m_numImage,QRectF(99.0,0.0,9.0,12.0));
    }
    else
    {
        painter->drawPixmap(QRectF(0.0,0.0,9.0,12.0),m_numImage,QRectF(90.0,0.0,9.0,12.0));
    }

    for(int i = 1; i < m_numarr.count();++i)
    {
        painter->drawPixmap(QRectF((m_numarr.count() - i) * 9,0.0,9.0,12.0),m_numImage,
                            QRectF(QRectF(m_numarr[i] * 9,0.0,9.0,12.0)));
    }
}

QRectF NumberItem::boundingRect() const
{
    return QRectF();
}
